<template>
  <el-table height="65vh" :data="questionlist" style="width: 100%">
    <el-table-column type="expand">
      <template #default="props">
        <div v-if="props.row.result">
          <table class="table table-borderless">
            <thead>
              <tr>
                <th scope="col">示例输入</th>
                <th scope="col">示例输出</th>
                <th scope="col">结果</th>
                <th scope="col">耗时</th>
                <th scope="col">内存</th>
                <th scope="col">判断</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(v, i) in props.row.info" :key="i">
                <td>
                  <pre>{{ v.input }}</pre>
                </td>
                <td>
                  <pre>{{ v.output }}</pre>
                </td>
                <td>
                  <pre>{{ v.stdout }}</pre>
                </td>
                <td>{{ v.t }}ms</td>
                <td>{{ memory(v.result, v) }}</td>
                <td v-text="stderr(v)"></td>
              </tr>
            </tbody>
          </table>
        </div>

        <pre v-else v-text="props.row.info.msg"></pre>
      </template>
    </el-table-column>
    <el-table-column width="150" prop="userid" label="用户id"></el-table-column>
    <el-table-column prop="questionid" label="题单ID"></el-table-column>
    <el-table-column prop="questionbankid" label="题单ID"></el-table-column>
    <!-- <el-table-column width="150" prop="userid" :formatter="df" label="得分"> -->
    <!-- </el-table-column> -->
    <el-table-column
      prop="date"
      label="提交时间"
      :formatter="formatter"
    ></el-table-column>
    <el-table-column
      prop="result"
      label="是否编译"
      :formatter="({ result }) => (result ? '是' : '否')"
    ></el-table-column>
  </el-table>
</template>
<script lang="ts">
import { defineComponent, PropType } from "vue";
import { toLocaleDateString } from "../../../util/time";
import Isstderr from "../../../util/isstderr";
import study from "../../../mysql_interface/studycodemodel";
import Studycodemodel from "../../../mysql_interface/studycodemodel";

export default defineComponent({
  name: "userquestion",
  props: {
    questionlist: {
      type: Array as PropType<Studycodemodel.Iuserquestion[]>,
      default: () => [],
    },
  },
  methods: {
    stderr(row: study.Iuserquestioninfo) {
      return Isstderr(row).msg;
    },
    toLocaleDateString,
    formatter(row: study.Iuserquestion) {
      return toLocaleDateString(row.createdAt);
    },
    memory(
      esult: study.Iuserquestion["result"],
      info: study.Iuserquestioninfo
    ) {
      if (!esult) return NaN;
      if (typeof info.memory === "number") {
        return info.memory / 1024 < 1
          ? Math.floor(info.memory) + "kb"
          : Math.floor(info.memory / 1024) + "mb";
      }
      return info.memory || "456kb";
    },
    df(row: study.Iuserquestion) {
      const { scorecalculation, result, type, info } = row;
      if (type === "代码" && result) {
        if (Array.isArray(info)) {
          const f = info.map(({ output, stdout, stderr, score }) => {
            if (stderr || output !== stdout) {
              return 0;
            }
            return Number(score);
          });
          /*eslint no-case-declarations: "off"*/
          switch (scorecalculation) {
            case "max":
              return f.sort((a, b) => Number(b) - Number(a))[0];
            case "min":
              return f.sort((a, b) => Number(a) - Number(b))[0];
            case "maxmin":
              const tmp = f.sort((a, b) => Number(b) - Number(a));
              return (Number(tmp[0]) + Number(tmp[tmp.length - 1])) / 2;
            default:
              let data = 0;
              for (const v of f) {
                data = data + Number(v);
              }
              return data;
          }
        }
      }
      return;
    },
  },
});
</script>